import $service from '../../../../service.js';
var doctor_id;
var hospital_id;
var log_id;
var that;
const app = getApp();
Page({
    data:{
       doctorList :[],
       isshow : false,
       activenum:-1,
       departmentList:[],
       department_id:'',
       doctor_name:''
    },
    onLoad(e){
        log_id = e.log_id;
    },
    onShow() {
        that =this.data;
        this.getOnlineDoctorList();
        this.getDepartmentListPc();
    },
    //医生列表
    getOnlineDoctorList() {
        let params = {
            "departmentId": that.department_id,
            "keyWord": that.doctor_name,
          }
        app.http.getOnlineDoctorList(params).then(res => {
            let list = $service.deepClone(res.data.list);
            this.setData({
                doctorList: list.filter(item => item.id !== JSON.parse($service.getStore('userInfo')).id)
            })
        })
    },
    // 科室列表
    getDepartmentListPc() {
        app.http.getDepartmentListPc().then(res => {
            this.setData({
                departmentList: res.data.list
            })
        })
    },
    //监听input框
    inputChange(e){
        this.setData({
            doctor_name : e.detail.value
        })
    },
    //搜索
    qr_Btn(){
        this.getOnlineDoctorList();
        this.setData({
            isshow:false
        })
    },
    //弹窗
    tapactive(e){
        console.log(e)
        var num = e.currentTarget.dataset.num;
        if(num == 0){
            this.data.isshow = !this.data.isshow
            this.setData({
                isshow : this.data.isshow
            })
        }else{
            var index = e.currentTarget.dataset.index;
            if(index == -1){
                 that.department_id= ''
            }else{
                 that.department_id= that.departmentList[index].id
            }
            this.setData({
                activenum : index,
                department_id: that.department_id
            })
        }
        
    },
    //分诊
    addTriageNote(e){
        var index = e.currentTarget.dataset.index;
        var index = e.currentTarget.dataset.index;
        app.webSocket.sendWebSocketMsg({
            data: {
                "logId": log_id, // 被分诊的问诊id
                "dispatchedDoctorId": that.doctorList[index].id,  // 被分诊的医生id     
                "packageType": 15
              },
            success: () => {
                $service.showToast('以发送申请')
            },
            fail: () => {
                $service.showToast('申请失败')
            }
        })
        app.webSocket.onReceivedMsg(res => {
            if (res.packageType === 16) {
                wx.showToast({"title":'分诊成功',"icon":"none",duration: 3000});
                setTimeout(function() {
                    wx.reLaunch({
                    url: '/pages/doctor_side/index/index'
                    })
                }, 1500);
            }
        })
    },
})